package com.behome.netty.netty_D.bean;

import com.behome.netty.common.ByteConventerKit;
import com.behome.netty.common.util.CRCKit;

import java.nio.ByteBuffer;
import java.util.Date;

public class PacketTail {
    static final short signLength = 74;
    static final short MIN_LENGTH = 6;
    Date signTime;
    String certNo;
    byte[] signData;
    byte[] crc32;
    boolean isSign = false;
    boolean signature = true;
    Packet packet;
    public PacketTail() {
    }

    public PacketTail(boolean isSign, Packet packet) {
        this.isSign = isSign;
        this.packet = packet;
    }

    public static short getLength(boolean isSign) {
        return (short)(isSign?80:6);
    }

    public Date getSignTime() {
        return this.signTime;
    }

    public void setSignTime(Date signTime) {
        this.signTime = signTime;
    }

    public String getCertNo() {
        return this.certNo;
    }

    public void setCertNo(String certNo) {
        this.certNo = certNo;
    }

    public byte[] getSignData() {
        return this.signData;
    }

    public void setSignData(byte[] signData) {
        this.signData = signData;
    }

    public byte[] getCrc32() {
        return this.crc32;
    }

    public void setCrc32(byte[] crc32) {
        this.crc32 = crc32;
    }

    public void setSignature(boolean signature){this.signature = signature;}
    public boolean getSignature(){return this.signature;}

    public byte[] serialize() {
        byte[] headData = this.packet.head.serialize();
        byte[] bodyData = this.packet.body.serialize();
        byte[] tmpData = new byte[headData.length + bodyData.length];
        System.arraycopy(headData, 0, tmpData, 0, headData.length);
        System.arraycopy(bodyData, 0, tmpData, headData.length, bodyData.length);
        byte[] signData = this.serializeSign(tmpData);
        ByteBuffer byteBuffer = ByteBuffer.allocate(headData.length + bodyData.length + signData.length);
        byteBuffer.put(headData).put(bodyData).put(signData);
        byte[] crcData = this.getCrc32Value(byteBuffer.array());
        byteBuffer = ByteBuffer.allocate(signData.length + crcData.length);
        byteBuffer.put(signData).put(crcData);
        return byteBuffer.array();
    }

    private byte[] serializeSign(byte[] data) {
        return ByteConventerKit.shortToBytes((byte)0);
//        if(!this.isSign) {
//            return ByteConventerKit.shortToBytes((byte)0);
//        } else {
//            byte[] signData = CryptoFactory.getCrypto().signature(data);
//            byte[] ret = new byte[2 + signData.length];
//            System.arraycopy(ByteConventerKit.shortToBytes((new Integer(signData.length)).shortValue()), 0, ret, 0, 2);
//            System.arraycopy(signData, 0, ret, 2, signData.length);
//            return ret;
//        }
    }

    public static PacketTail deserialize(byte[] srcData,byte[] tailData,boolean isSign) {
        PacketTail ret = new PacketTail();
//        if(tailData != null || tailData.length > 0) {
//            if(isSign){
//                ret.setSignature(CryptoFactory.getCrypto().verifySignature(srcData,tailData));
//            }
//        }
        return ret;
    }

    private byte[] getCrc32Value(byte[] data) {
        return ByteConventerKit.intToBytes2(CRCKit.crc32Calc(data));
    }

}
